cmake_minimum_required(VERSION 3.8.0 FATAL_ERROR)

function(set_using_dynamic_name VAR_NAME VALUE)
    set("${VAR_NAME}" "${VALUE}" PARENT_SCOPE)
endfunction()

macro(core_parse_version VERSION_TEXT OUPUT_PREFIX)
    set(VERSION_REGEX "[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+(-[a-zA-Z0-9_]+)?")
    if( ${VERSION_TEXT} MATCHES ${VERSION_REGEX} )
        string(REGEX MATCHALL "[0-9]+|-([A-Za-z0-9_]+)" VERSION_PARTS ${VERSION_TEXT})
        list(GET VERSION_PARTS 0 ${OUPUT_PREFIX}_PART0)
        list(GET VERSION_PARTS 1 ${OUPUT_PREFIX}_PART1)
        list(GET VERSION_PARTS 2 ${OUPUT_PREFIX}_PART2)
        list(GET VERSION_PARTS 3 ${OUPUT_PREFIX}_PART3)
        set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" TRUE )
    else()
        set_using_dynamic_name( "${OUPUT_PREFIX}_FOUND" FALSE )
    endif()
endmacro()

string(TIMESTAMP VERSION_BUILD_PTS %Y%m%d%H%M%S)
message(STATUS "Build timestamp: ${VERSION_BUILD_PTS}")

exec_program(
    "git"
    ${CMAKE_CURRENT_SOURCE_DIR}
    ARGS "log --format=%h -1"
    OUTPUT_VARIABLE VERSION_GIT_SHA1)

exec_program(
    "git"
    ${CMAKE_CURRENT_SOURCE_DIR}
    ARGS "rev-list --tags --max-count=1"
    OUTPUT_VARIABLE VERSION_TAG_SHA1)

exec_program(
    "git"
    ${CMAKE_CURRENT_SOURCE_DIR}
    # ARGS "describe --tags ${VERSION_TAG_SHA1}"
    ARGS "describe --tags --abbrev=4 ${VERSION_GIT_SHA1}"
    OUTPUT_VARIABLE VERSION_TAG_DESCRIBE)

set(VERSION_TAG_REGEX "v[0-9]+.[0-9]+.[0-9]+.[0-9]+-[0-9]+")
if(${VERSION_TAG_DESCRIBE} MATCHES ${VERSION_TAG_REGEX})
    string (REGEX REPLACE ${VERSION_TAG_REGEX} ""
        TAG_SHA1_PREFIX
        ${VERSION_TAG_DESCRIBE})

    message(STATUS "VERSION_TAG_DESCRIBE: ${VERSION_TAG_DESCRIBE}")
    string (REGEX REPLACE "${TAG_SHA1_PREFIX}" ""
        VERSION_GIT_TAG
        ${VERSION_TAG_DESCRIBE})
else()
    set(VERSION_GIT_TAG ${VERSION_TAG_DESCRIBE})
endif()
message(STATUS "VERSION_GIT_TAG: ${VERSION_GIT_TAG} VERSION_GIT_SHA1: ${VERSION_GIT_SHA1}")

core_parse_version(${VERSION_GIT_TAG} VERSION)

if(NOT VERSION_FOUND)
    message(FATAL_ERROR "Failed to parse version tag string properly. Expect v[1-9].[0-9].[0-9].[0-9]-[a-zA-Z0-9]")
endif()

message(STATUS "Core Version: ${VERSION_PART0}.${VERSION_PART1}.${VERSION_PART2}.${VERSION_PART3}")

set(VERSION_GIT 1)
file(WRITE ${CYG_CPP_ROOT}/version.h
    "#ifndef __VERSION_H__\n"
    "#define __VERSION_H__\n"
    "#define VERSION_COPYRIGHT \t\t\"Contron, Ltd. <caiwang213@qq.com>\"\n"
    "#define VERSION_BUILD_PTS \t\t\"${VERSION_BUILD_PTS}\"\n\n"

    "#define VERSION_GIT       \t\t${VERSION_GIT}\n"
    "#define VERSION_GIT_TAG   \t\t\"${VERSION_GIT_TAG}\"\n"
    "#define VERSION_GIT_SHA1  \t\t\"${VERSION_GIT_SHA1}\"\n\n"

    "#define VERSION_SVN       \t\t${VERSION_SVN}\n"
    "#define VERSION_SVN_ALL   \t\t\"${VERSION_SVN_ALL}\"\n"
    "#define VERSION_SVN_TAG   \t\t\"${VERSION_PART0}.${VERSION_PART1}.${VERSION_PART2}.${VERSION_PART3}\"\n"
    "#define VERSION_SVN_REV   \t\t\"r${VERSION_SVN_REV}\"\n\n"

    "#if VERSION_GIT\n"
    "  #define VERSION_TAG     \t\tVERSION_GIT_TAG\n"
    "  #define VERSION_REV     \t\tVERSION_GIT_SHA1\n"
    "#else\n"
    "  #define VERSION_TAG     \t\tVERSION_SVN_TAG\n"
    "  #define VERSION_REV     \t\tVERSION_SVN_REV\n"
    "#endif\n\n"

    "#define VERSION_PART0     \t\t${VERSION_PART0}\n"
    "#define VERSION_PART1     \t\t${VERSION_PART1}\n"
    "#define VERSION_PART2     \t\t${VERSION_PART2}\n"
    "#define VERSION_PART3     \t\t${VERSION_PART3}\n\n"

    "#define VERSION_COMPILER  \t\t\"${CMAKE_CXX_COMPILER_ID}-${CMAKE_CXX_COMPILER_VERSION}\"\n"

    "#endif\n"
    )
